<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <h1>微博首页</h1>
    <div v-if="isLogin"><!--登录过显示-->
        <h3>欢迎{{user.nickname}}回来</h3>
        <a href="/send.html">发微博</a>
        <a href="javascript:void(0)" @click="logout()">退出登录</a>
    </div>
    <div v-else><!--没有登录显示-->
        <a href="/reg.html">注册</a>
        <a href="/login.html">登录</a>
    </div>
    <hr>
    <div v-for="weibo in arr">
        <a :href="'/detail.html?id='+weibo.id">{{weibo.nickname}}说:{{weibo.content}}</a>

        <span style="font-size: 12px">{{weibo.created}}</span>
    </div>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.min.js"></script>
<script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>
<script>
    let v = new Vue({
        el: "body>div",
        data: {
            user: {},
            isLogin:false,
            arr:[]
        },
        methods: {
            logout(){
                //发出退出登录的请求
                axios.get("/logout").then(function () {
                    v.isLogin = false;
                })

            }
        },
        created:function () {
            //发出获取所有微博的请求
            axios.get("/weibo/select").then(function (response) {
                v.arr = response.data;
            })


            //发请求获取当前登录的用户对象
            axios.get("/currentUser").then(function (response) {
                //服务器响应的数据有可能是user对象 也有可能是空字符串(服务器响应的是null但是传输到客户端后变成了"")
                v.user = response.data;
                // if (v.user==""){
                //     v.isLogin = false;
                // }else{
                //     v.isLogin = true;
                // }
                v.isLogin = v.user==""?false:true;
            })
        }
    })
</script>
</body>
</html>